import Vue from 'vue'
import Vuex, { Store } from 'vuex'
import { mapKeys } from 'lodash'
import feth from '../utils/fetch.js'
import { async } from 'q';

Vue.use(Vuex)

let files = require.context("./", false, /\.js$/)
let modules = files.keys().reduce((modules, path) => {
  if (path === './index.js') return modules;
  let name = path.slice(2,-3) 
  let {state = {}, mutations = {}, actions = {}} = files(path).default
  modules[name] = {
    state,
    mutations: mapKeys(mutations, (val, key) => name + '/' + key),
    actions: mapKeys(actions, (val, key) => name + '/' + key)
  }
  return modules
}, {})

export default new Store({
  actions: {
    fetch: (ctx, {url, params, config}) => feth(url, params, config)
  },
  modules
})